﻿#ifndef DMUTIL_H
#define DMUTIL_H
#include "dmos.h"

static inline struct tm* localtime_r(const time_t* timep,struct tm* result)
{
    localtime_s(result,timep);
    return result;
}
static inline struct tm* gmtime_r( const time_t* timep, struct tm* result )
{
    gmtime_s( result, timep );
    return result;
}

static inline std::string DMFormatIP(unsigned int dwIP)
{
    sockaddr_in s;
    s.sin_family= AF_INET;
    s.sin_addr.s_addr = dwIP;
    return inet_ntoa(s.sin_addr);
}
static inline unsigned int DMFormatIP(const std::string &strIP)
{
    return inet_addr(strIP.c_str());
}
static inline unsigned short DMFormatPort( const std::string& strPort )
{
    return htons( atoi( strPort.c_str() ) );
}
static inline unsigned short DMFormatPort( unsigned short wPort )
{
    return htons( wPort );
}

static inline std::string DMFormatDateTime( time_t tVal = time( 0 ),
                                           const char* pFormat = "%Y-%m-%d %H:%M:%S" )
{
    struct tm Tm = {0};

    if ( localtime_r( &tVal, &Tm ) )
    {
        char szBuf[128];
        strftime( szBuf, sizeof( szBuf ), pFormat, &Tm );
        return szBuf;
    }

    return "";
}
static inline time_t DMFormatDateTime( const std::string& strTime,
                                      const char* pFormat =  "%d-%d-%d %d:%d:%d" )
{
    time_t ret = 0;
    struct tm tmMake = {0};

    if ( 6 == sscanf( strTime.c_str(), pFormat, &tmMake.tm_year, &tmMake.tm_mon,
                    &tmMake.tm_mday, &tmMake.tm_hour, &tmMake.tm_min, &tmMake.tm_sec ) )
    {
        tmMake.tm_year -= 1900;
        tmMake.tm_mon -= 1;
        ret = mktime( &tmMake );
    }

    return ret;
}

static bool DMIsDirectory(const char* dir_name)
{
    int ret= GetFileAttributesA(dir_name);
    if(ret == -1)
        return false;

    return !!(FILE_ATTRIBUTE_DIRECTORY & ret);
}

static inline bool DMCreateDirectory(const char* dir_name)
{
    int ret= mkdir(dir_name);
    if(0 != ret)
    {
        return false;
    }

    return true;
}

static inline bool DMCreateDirectorys(const char* dir_name)
{
    if(access(dir_name,0)==0)
    {
        if(DMIsDirectory(dir_name))
        {
            return true;
        }
        return false;
    }
    char path[MAX_PATH];
    strncpy(path,dir_name,sizeof(path));
    char* p = strrchr(path,PATH_DELIMITER);
    if(NULL==p)
    {
        return DMCreateDirectory(path);
    }
    *(p)='\0';
    DMCreateDirectorys(path);
    return DMCreateDirectory(dir_name);

}

static std::string DMGetRootPath()
{
    static char path[MAX_PATH];
    static bool first_time=true;
    if(first_time)
    {
        first_time=false;
        GetModuleFileNameA(0,path,sizeof(path));
        char* p= strrchr(path,'\\');
        *(p)='\0';
    }
    return path;
}
static std::string DMGetExePath()
{
    static char path[MAX_PATH];
    static bool first_time=true;
    if(first_time)
    {
        first_time=false;
        GetModuleFileNameA(0,path,sizeof(path));
    }
    return path;
}

static  std::string DMGetWorkPath()
{
    char szPath[MAX_PATH];
    getcwd(szPath,sizeof(szPath));
    return szPath;
}
#endif // DMUTIL_H
